import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'


Vue.use(VueRouter)

const routes = [
  // 登录路由
  { name:'login', path:'/login', component: ()=>import('@/views/login'),meta:{title:'登录'} },
  // 主体框架
  { 
    // 如果父级路由指定到某个子集路由那么，可以不用写name
    // name:'layout', 
    path:'/', 
    component: ()=>import('@/views/layout'),
    children:[
      { name:'home', path:'/', component: ()=> import('@/views/home'),meta:{needLogin:true,title:'首页'} },
      { name:'wd', path:'/wd', component: ()=> import('@/views/wd'),meta:{title:'问答'} },
      { name:'video', path:'/video', component: ()=> import('@/views/video'),meta:{title:'视频'} },
      { name:'user', path:'/user', component: ()=> import('@/views/user'),meta:{title:'我的'} },
    ] 
  },
  {
    name:'search',path: '/search',component: ()=> import('@/views/search'),meta:{title:'搜索'}
  },
  {
    name:'article',path: '/article/:articleId',component: ()=> import('@/views/article'),props:true,meta:{title:'文章详情'}
  },
  { name:'userProfile',path: '/user/profile',component: ()=> import('@/views/user-profile'),meta:{ title:'资料修改'}}

]


const router = new VueRouter({
  routes
})
router.beforeEach((to,form,next)=>{
  document.title = to.meta.title
  if(to.meta.needLogin){
    if(store.state.user){
      next()
    }else{
      router.push(`/login?url=${to.path}`)
    }
  }else{
    next()
  }
})
export default router
